library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3     ✓ purrr   0.3.4
## ✓ tibble  3.0.4     ✓ dplyr   1.0.2
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(janitor)
## 
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(palmerpenguins)
library(here)
## here() starts at /Users/gracebrofman/Desktop/ESM244 Adv. Data/Labs/Lab 2/esm244-w2021-lab2
# For PCA:
library(ggfortify)

# For ggplot customization:
library(readxl)
library(gghighlight)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
# To make compound figures:
library(patchwork)

Part 1: PCA with penguins

penguin_pca <- penguins %>% 
  select(body_mass_g, ends_with("_mm")) %>% 
  drop_na() %>% 
  scale() %>% 
  prcomp()

penguin_pca$rotation 
##                          PC1         PC2        PC3        PC4
## body_mass_g        0.5483502 0.084362920 -0.5966001 -0.5798821
## bill_length_mm     0.4552503 0.597031143  0.6443012 -0.1455231
## bill_depth_mm     -0.4003347 0.797766572 -0.4184272  0.1679860
## flipper_length_mm  0.5760133 0.002282201 -0.2320840  0.7837987
penguin_complete <- penguins %>% 
  drop_na(body_mass_g, ends_with("_mm"))

# create a biplot of PCA

autoplot(penguin_pca,
         data = penguin_complete,
         colour = "species",
         loadings = TRUE,
         loadings.label = TRUE) +
  theme_minimal()
## Warning: `select_()` is deprecated as of dplyr 0.7.0.
## Please use `select()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

Part 2: ggplot2 customization and reading in different file types

Read in an .xlsx file and do some wrangling

fish_noaa <- read_excel(here("data", "foss_landings.xlsx")) %>% 
  clean_names() %>% 
  mutate(across(where(is.character), tolower)) %>% 
  mutate(nmfs_name = str_sub(nmfs_name, end = -4)) %>% 
  filter(confidentiality == "public")

Make a customized graph:

fish_plot <- ggplot(data = fish_noaa,
                    aes(x = year,
                        y = pounds)) +
  geom_line(aes(color = nmfs_name),
            show.legend = FALSE) +
  theme_minimal()

fish_plot
## Warning: Removed 6 row(s) containing missing values (geom_path).

ggplotly(fish_plot)